//
//  FavoriteCollectionViewController.m
//  Car
//
//  Created by 何晓文 on 15/10/5.
//  Copyright © 2015年 朱鸿儒. All rights reserved.
//

#import "FavoriteCollectionViewController.h"
#import "VVSpringCollectionViewFlowLayout.h"
#import "BeautyModel.h"
#import "MyCollectionViewCell.h"
#import "BeautyDetailViewController.h"
@interface FavoriteCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation FavoriteCollectionViewController


static NSString * const reuseIdentifier = @"collCell";
/**
 *  初始化布局
 */
- (instancetype)init
{
	UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
	layout.itemSize = CGSizeMake(kWidth,80);
	return [self initWithCollectionViewLayout:layout];
}
- (void)viewDidLoad {
    [super viewDidLoad];
	self.collectionView.backgroundColor = [UIColor buttermilkColor];
	self.dataArray = [[MyFavorite shareFavorite] extractDataWithUser:[EnterInformationHandle share].myNumber];
		    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	
    [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
	BeautyModel *model = self.dataArray[indexPath.row];
	cell.titleLabel.text = model.title;
	NSURL *url = [NSURL URLWithString:model.smallpic];
	[cell.imageView sd_setImageWithURL:url];
	//给cell添加手势
	UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteItems:)];
	//先添加手势
	[cell addGestureRecognizer:swipe];

    return cell;
}
//删除对应item
- (void)deleteItems:(UISwipeGestureRecognizer *)recognizer {
	if (recognizer.state == UIGestureRecognizerStateEnded) {
		//通过手势获取当前的indexPath
		 NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[recognizer locationInView:self.collectionView]];
		BeautyModel *model = self.dataArray[indexPath.row];
		
		CGFloat newX = recognizer.view.x + kWidth;
		[UIView animateWithDuration:0.5 animations:^{
			recognizer.view.frame = CGRectMake(newX, recognizer.view.y, recognizer.view.width, recognizer.view.height);
			recognizer.view.alpha = 0;
		} completion:^(BOOL finished) {
			//删除数据库
			[[MyFavorite shareFavorite] deleteBeautyByTopicid:model.topicid WithUser:[EnterInformationHandle share].myNumber];
			// 删UI(刷新UI)

			[self.dataArray removeObjectAtIndex:recognizer.view.tag];
			[self.collectionView deleteItemsAtIndexPaths: @[indexPath]];
			[self.collectionView reloadData];
		}];
		
	}

}
#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	NSLog(@"点了");
	BeautyModel *model = self.dataArray[indexPath.row];
	
	BeautyDetailViewController *detail = [[BeautyDetailViewController alloc] init];
	detail.webUrl = [NSString stringWithFormat:kShowURL,model.topicid];
	detail.model = model;
	[self presentViewController:detail animated:YES completion:nil];
}

- (NSMutableArray *)dataArray {
	if (!_dataArray) {
		_dataArray = [[NSMutableArray alloc] init];
	}
	return _dataArray;
}



@end
